package com.example.demo.vo;


import org.hibernate.criterion.Order;

import java.io.Serializable;
import java.util.Arrays;

/**
 * 分页请求数据
 *
 * @author Jack
 * @version V1.0
 * @Title:
 * @Description: 分页请求数据
 * @date 2018/1/12 16:31
 */
public class PageRequest implements Serializable {

    private static final long serialVersionUID = -389729438941722690L;
    /* 开始条数 */
    private int offset = 0;
    /* 每页显示条数 */
    private int limit = 10;
    /* 排序方式数组 */
    private Order[] order = null;
    /* 是否计算总条数，如果不计算可以提高效率 */
    private boolean countTotal = true;
    /* 当前页 */
    private int currentPage = 1; // 当前页

    public int getCurrentPage() {
        return currentPage;
    }

    public void setCurrentPage(int currentPage) {
        this.currentPage = currentPage <= 0 ? 1 : currentPage;
        this.offset = (currentPage - 1) * limit;
    }

    public PageRequest() {
    }

    /**
     * @param offset 跳过行数(从0开始)
     * @param limit  限制查询的条数
     * @param order  不传,传null或new Order[]{}空数组,均默认id降序处理
     */
    public PageRequest(int offset, int limit, Order... order) {
        this.setOffset(offset);
        this.setLimit(limit);
        this.setOrder(order);
    }


    public int getOffset() {
        return this.offset;
    }

    /**
     * 设置跳过行数(从0开始),低于0时自动调整为0.
     */
    public void setOffset(final int offset) {
        this.offset = offset < 0 ? 0 : offset;
        this.currentPage = this.offset / this.limit + 1;
    }

    /**
     * 获得查询限制的记录数量
     */
    public int getLimit() {
        return this.limit;
    }

    /**
     * 设置限制查询的记录数量
     */
    public void setLimit(final int limit) {
        this.limit = limit < 0 ? 10 : limit;
    }


    public boolean isCountTotal() {
        return countTotal;
    }

    public void setCountTotal(boolean countTotal) {
        this.countTotal = countTotal;
    }

    public Order[] getOrder() {
        return order == null ? null : Arrays.copyOf(order, order.length);
    }

    public void setOrder(Order[] order) {
        if (order != null && order.length > 0) {
            this.order = Arrays.copyOf(order, order.length);
        } else {
            this.order = null;
        }
    }

}
